package xiaoqi.qingyue.api.basic.photo.controller;

import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import xiaoqi.qingyue.api.basic.photo.model.entity.Photo;
import xiaoqi.qingyue.api.basic.photo.service.PhotoService;
import xiaoqi.qingyue.api.basic.user.controller.UserController;
import xiaoqi.qingyue.api.common.Result;
import xiaoqi.qingyue.api.utils.UploadUtil;

import java.util.List;

/**
 * @author 92110
 */
@CrossOrigin
@RestController
@RequestMapping("/photo")
@RequiredArgsConstructor
public class PhotoController {

    private final UploadUtil uploadUtil;
    private final PhotoService photoService;
    private static final Logger logger = LoggerFactory.getLogger(PhotoController.class);

    @PostMapping("/upload")
    public Result<Photo> upload(@RequestBody MultipartFile file) {
        try {
            Photo photo = uploadUtil.uploadFile(file);
            return Result.success("上传成功", photoService.uploadPhoto(photo));
        } catch (Exception e) {
            logger.error("上传失败:{}", e.getMessage());
            return Result.error("上传失败");
        }
    }

    @GetMapping("/list")
    public Result<List<Photo>> getPhotoList() {
        try {
            return Result.success("查询成功", photoService.getPhotoList());
        } catch (Exception e) {
            return Result.error("查询失败");
        }
    }

    @GetMapping("/listAll")
    public Result<List<Photo>> getPhotoListAll() {
        try {
            return Result.success("查询成功", photoService.getPhotoListAll());
        } catch (Exception e) {
            return Result.error("查询失败");
        }
    }

    @DeleteMapping("/delete/{id}")
    public Result<String> deletePhoto(@PathVariable String id) {
        try {
            photoService.deletePhoto(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error("删除失败");
        }
    }
}
